home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11675 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  71 lines

  1. Path: news.mindspring.com!usenet
  2. From: rudd@mindspring.com (Justin Rudd)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Can you overload a constructor?
  5. Date: Fri, 15 Mar 1996 17:22:39 GMT
  6. Organization: MindSpring Enterprises
  7. Message-ID: <4ic92p$lj6@B1FF.mindspring.com>
  8. References: <Do859K.K9B@watserv3.uwaterloo.ca> <31483CDC.4B0C@staff.ichange.com> <4ibqan$br5@si-nic.hrz.uni-siegen.de>
  9. Reply-To: rudd@mindspring.com
  10. NNTP-Posting-Host: rudd.mindspring.com
  11. X-Newsreader: Forte Free Agent v0.55
  12.  
  13. plrunu@informatik.uni-siegen.de (Runu Knips) wrote:
  14.  
  15. >Sorry, it is impossible to OVERLOAD a constructor. A constructor
  16. >is ALWAYS called, if there is one for a class. And before it
  17. >get called, any constructor for superclasses get called.
  18.  
  19. um...actually you can overload constructors.  Of course they do get
  20. called but not always.  But first lets looks at this...
  21.  
  22. class Foo
  23. {
  24.     public:
  25.         Foo();
  26.         Foo( int i );
  27.         Foo( float f );
  28.         Foo( double d );
  29.  
  30.         void Get( int& i );
  31.         void Get( float& f );
  32.         void Get( double& d );
  33.  
  34. };
  35.  
  36. There I have 4 different OVERLOADED ways to initialize an object of
  37. type Foo.  And also 3 different OVERLOADED ways to get the value of
  38. Foo.
  39.  
  40. Now as for constructors always being called.  What if I have this:
  41.  
  42. class Graphic
  43. {
  44.     public:
  45.         virtual void Draw() = 0;
  46. };
  47.  
  48. class Box : public Graphic
  49. {
  50.     public:
  51.         virtual void Draw();
  52. };
  53.  
  54. void main()
  55. {
  56.     Graphic* graph;        
  57.     Box box;
  58.  
  59.     graph = &box;
  60.  
  61.     graph->Draw();
  62. }
  63.     
  64. In this little code segment here I have a POINTER to a Graphic object.
  65. It has not been initialized..therefore no constructor call.  And then
  66. after that...I make the Graphic pointer point to a Box object...so at
  67. no time was the Graphic pointer an instance of Graphic.
  68.  
  69. Justin
  70.  
  71.